package com.wxmp.wxapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wxmp.core.util.DateUtil;
import com.wxmp.wxapi.service.IAccountService;
import com.wxmp.wxapi.util.CaiHongPi;
import com.wxmp.wxapi.util.Pusher;
import com.wxmp.wxapi.util.Tianqi;
import com.wxmp.wxcms.domain.Account;
import com.wxmp.wxcms.domain.AccountFans;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Description
 * @Author cys
 * @Date 2022/10/8 10:47 AM
 */
@Service("cys")
public class CysAccountService extends AbstractAccountService implements IAccountService {

    public static final String cysBirthday = "1995-02-21";
    public static final String lfBirthday = "1995-03-27";

    @Override
    public void pushMorningMessage(AccountFans accountFans, String templateId, Account account) {
        //1，配置
        WxMpService wxMpService = Pusher.getWxStorage(account);
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(accountFans.getOpenId())
                .templateId(templateId)
                //.url("https://30paotui.com/")//点击模版消息要访问的网址
                .build();

        JSONObject todayWeather = Tianqi.getNanjiTianqi(accountFans.getCity(), account.getBaiduak());
        String textDay = todayWeather.getString("text_day");
        Integer high = todayWeather.getInteger("high");
        Integer low = todayWeather.getInteger("low");
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate nowDate = LocalDate.now();
        String localDateStr = nowDate.format(fmt);
        templateMessage.addData(new WxMpTemplateData("date", localDateStr + " " + DateUtil.getDayOfWeek(nowDate)));
        templateMessage.addData(new WxMpTemplateData("city", accountFans.getProvince(), "#ff66cc"));
        templateMessage.addData(new WxMpTemplateData("weather", textDay, "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("min_temperature", low + "℃", "#ff00ff"));
        templateMessage.addData(new WxMpTemplateData("max_temperature", high + "℃", "#ff0075"));
        String loveDate = StringUtils.isNotBlank(accountFans.getLoveDate()) ? accountFans.getLoveDate() : localDateStr;

        if ("love".equals(accountFans.getRemark())) {
            templateMessage.addData(new WxMpTemplateData("love_day", "今天是我们恋爱的第" + DateUtil.daysBetween(loveDate, DateUtil.changeDateTOStr3(new Date())) + "天", "#FF0000"));
            templateMessage.addData(new WxMpTemplateData("birthday1", "距离谌十二的生日还有" + DateUtil.getBirthDay(cysBirthday) + "天", "#ed6e6e"));
            templateMessage.addData(new WxMpTemplateData("birthday2", "距离丽宝的生日还有" + DateUtil.getBirthDay(lfBirthday) + "天", "#ab3ef3"));
        } else {
            templateMessage.addData(new WxMpTemplateData("love_day", CaiHongPi.getJinJu(), "#FF0000"));
        }

        JSONObject object = CaiHongPi.getEveryday(account.getChpkey(), CaiHongPi.everydayUrl);
        templateMessage.addData(new WxMpTemplateData("note_en", object.getString("content"), "#C71585"));
        templateMessage.addData(new WxMpTemplateData("note_zn", object.getString("note"), "#C71585"));
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
